package org.locker.service.impl;

import org.locker.common.core.utils.MapstructUtils;
import org.locker.common.core.utils.StringUtils;
import org.locker.common.mybatis.core.page.TableDataInfo;
import org.locker.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.locker.domain.bo.DeviceUploadLogBo;
import org.locker.domain.vo.DeviceUploadLogVo;
import org.locker.domain.DeviceUploadLog;
import org.locker.mapper.DeviceUploadLogMapper;
import org.locker.service.IDeviceUploadLogService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 日志上传记录Service业务层处理
 *
 * @author winnie
 * @date 2023-10-14
 */
@RequiredArgsConstructor
@Service
public class DeviceUploadLogServiceImpl  extends ServiceImpl<DeviceUploadLogMapper,DeviceUploadLog>  implements IDeviceUploadLogService {

    private final DeviceUploadLogMapper baseMapper;

    /**
     * 查询日志上传记录
     */
    @Override
    public DeviceUploadLogVo queryById(Long deviceUploadLogId){
        return baseMapper.selectVoById(deviceUploadLogId);
    }

    /**
     * 查询日志上传记录列表
     */
    @Override
    public TableDataInfo<DeviceUploadLogVo> queryPageList(DeviceUploadLogBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<DeviceUploadLog> lqw = buildQueryWrapper(bo);
        Page<DeviceUploadLogVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询日志上传记录列表
     */
    @Override
    public List<DeviceUploadLogVo> queryList(DeviceUploadLogBo bo) {
        LambdaQueryWrapper<DeviceUploadLog> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<DeviceUploadLog> buildQueryWrapper(DeviceUploadLogBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<DeviceUploadLog> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getPositionId() != null, DeviceUploadLog::getPositionId, bo.getPositionId());
        lqw.eq(StringUtils.isNotBlank(bo.getDeviceNo()), DeviceUploadLog::getDeviceNo, bo.getDeviceNo());
        lqw.eq(StringUtils.isNotBlank(bo.getLogType()), DeviceUploadLog::getLogType, bo.getLogType());
        lqw.eq(StringUtils.isNotBlank(bo.getFileUrl()), DeviceUploadLog::getFileUrl, bo.getFileUrl());
        lqw.like(StringUtils.isNotBlank(bo.getFileName()), DeviceUploadLog::getFileName, bo.getFileName());
        return lqw;
    }

    /**
     * 新增日志上传记录
     */
    @Override
    public Boolean insertByBo(DeviceUploadLogBo bo) {
        DeviceUploadLog add = MapstructUtils.convert(bo, DeviceUploadLog.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setDeviceUploadLogId(add.getDeviceUploadLogId());
        }
        return flag;
    }

    /**
     * 修改日志上传记录
     */
    @Override
    public Boolean updateByBo(DeviceUploadLogBo bo) {
        DeviceUploadLog update = MapstructUtils.convert(bo, DeviceUploadLog.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(DeviceUploadLog entity){
        //做一些数据校验,如唯一约束
    }

    /**
     * 批量删除日志上传记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
